﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Management;
using System.Configuration;
using System.Net;
using System.IO;

namespace PictureProcessing.Comm
{
    public class CheckActivateCode
    {

        //获取电脑的mac地址然后MD5加密
        public static string GetMacAddress()
        {
            try
            {
                string strMac = string.Empty;
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        strMac = mo["MacAddress"].ToString();
                    }
                }
                moc = null;
                mc = null;
                return strMac;
            }
            catch
            {
                return "unknown";
            }
        }

        public static string GetAppSetting()
        {

            string str = "";
            str = ConfigurationManager.AppSettings["ActivateCode"];
            return str;
           
        }

        public static void SaveCode(string ActivateCode) 
        {
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings["ActivateCode"].Value = ActivateCode;
            cfa.Save();
            ConfigurationManager.RefreshSection("appSettings");
        }

        /// <summary>
        /// 检查激活码
        /// </summary>
        /// <returns></returns>
        public static bool CheckCode() 
        {
            bool result = false;
            string appActivateCode = GetAppSetting();
            if (string.IsNullOrEmpty(appActivateCode))
            {
                result = false;
                return result;
            }
            else 
            {
                string mac = GetMacAddress();
                string MD5mac = MD5(mac);
                if (MD5mac == appActivateCode)
                {
                    result = true;
                }
                else 
                {
                    result = false;
                }

            }
            return result;
        }


        public static bool CheckCode2(string code)
        {
            bool result = false;
          
            string mac = GetMacAddress();
            string MD5mac = MD5(mac);
            if (MD5mac == code)
            {
                result = true;
            }
            else
            {
                result = false;
            }
            return result;
        }

        public static String MD5(string str)
        {
            //char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
            char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(str);
            byte[] result = md5.ComputeHash(inputBytes);
            int j = result.Length;
            char[] chars = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++)
            {
                byte byte0 = result[i];
                chars[k++] = hexDigits[byte0 >> 4 & 0xf];
                chars[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(chars);
        }



        /// <summary>
    /// GET方法
    /// </summary>
    /// <param name="Url"></param>
    /// <returns></returns>
    public static string Get(string Url)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
        request.Proxy = null;
        request.KeepAlive = false;
        request.Method = "GET";
        request.ContentType = "application/json; charset=UTF-8";
        request.AutomaticDecompression = DecompressionMethods.GZip;

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream myResponseStream = response.GetResponseStream();
        StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
        string retString = myStreamReader.ReadToEnd();

        myStreamReader.Close();
        myResponseStream.Close();

        if (response != null)
        {
            response.Close();
        }
        if (request != null)
        {
            request.Abort();
        }

        return retString;
    }


    }
}
